/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.products.ctrl;


import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.products.entity.ProductsSold;
import com.pitop.products.service.ProductsSoldSVImpl;

/**
 * 已销售设备 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/productsSold")
@Slf4j
@Api(value = "已销售设备控制器", description = "已销售设备控制器")
public class ProductsSoldCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private ProductsSoldSVImpl productsSoldSVImpl;


    /**
     * 查询ProductsSold一个详情信息
     *
     * @param id   id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductsSold一个详情信息", notes = "查询ProductsSold一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        ProductsSold productsSold = productsSoldSVImpl.load(id, code);
        log.info(JSON.toJSONString(productsSold));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productsSold);
    }


    /**
     * 根据条件id查询ProductsSold一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductsSold一个详情信息", notes = "查询ProductsSold一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProductsSold productsSold = productsSoldSVImpl.loadById(id);
        log.info(JSON.toJSONString(productsSold));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productsSold);
    }

    /**
     * 根据条件code查询ProductsSold一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProductsSold一个详情信息", notes = "查询ProductsSold一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProductsSold productsSold = productsSoldSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(productsSold));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productsSold);
    }


    /**
     * 查询ProductsSold信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProductsSold信息集合", notes = "查询ProductsSold信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ProductsSold productsSold, @ApiIgnore Page<ProductsSold> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<ProductsSold> productsSolds = productsSoldSVImpl.list(productsSold, page.getCurPage(), page.getPageSize());
        int total = productsSoldSVImpl.count(productsSold);
        page.setTotalRow(total);
        page.setVoList(productsSolds);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 查询ProductsSold信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProductsSold信息集合", notes = "查询ProductsSold信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<ProductsSold> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<ProductsSold> productsSolds = productsSoldSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = productsSoldSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(productsSolds);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建ProductsSold
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ProductsSold", notes = "创建ProductsSold")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "sn", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore ProductsSold productsSold) {
        if (StringUtils.isEmpty(productsSold.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productsSold.getSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productsSold.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        productsSoldSVImpl.save(productsSold);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productsSold);
    }


    /**
     * 修改ProductsSold
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改ProductsSold", notes = "修改ProductsSold")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "sn", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore ProductsSold productsSold) {
        if (StringUtils.isEmpty(productsSold.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productsSold.getSn())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(productsSold.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        productsSoldSVImpl.modify(productsSold);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, productsSold);
    }

    /**
     * 根据主键删除ProductsSold
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除ProductsSold", notes = "删除ProductsSold")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        productsSoldSVImpl.delete(id, code);
        return BeanRet.create(true, "删除ProductsSold成功");
    }


}
